package com.system.service.impl;

import com.system.mapper.ContractGoodsMapper;
import com.system.mapper.InvoiceMapper;
import com.system.mapper.ProductMapper;
import com.system.pojo.Product;
import com.system.service.ProductService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductMapper productMapper;

	@Autowired
	private ContractGoodsMapper cgm;

	@Autowired
	private InvoiceMapper invoiceMapper;

	@Override
	public ResultVo<Product> insert(Product product) {
		ResultVo<Product> productVo = new ResultVo<Product>();
		// 判断该产品是否已经存在
		String productName = product.getProductName();
		Product product1 = productMapper.selectByProductName(productName);
		if (product1 == null) {
			// 可以插入新的产品
			productMapper.insertSelective(product);
			productVo.setInsert(true);
		} else {
			productVo.setMessage("该产品名称已存在");
		}
		return productVo;
	}

	@Override
	public ResultVo<Product> update(Product product) {
		ResultVo<Product> resultVo = new ResultVo<Product>();
//        Product product1 = productMapper.selectByProductName(product.getProductName());
//        if (product1 == null){
		int i = cgm.updateProductNameByProductId(product);
		int j = invoiceMapper.updateProductNameByProductId(product);
		int update = productMapper.updateByPrimaryKeySelective(product);
		
//		if (update == 1 && i == 1 && j == 1) {
		// 更行成功
		resultVo.setUpdate(true);
		resultVo.setMessage("更新产品信息成功");
//		} else {
//			resultVo.setMessage("更新产品信息失败");
//		}

		return resultVo;
	}

	@Override
	public ResultVo<Product> selectAll() {
		ResultVo<Product> productVo = new ResultVo<Product>();
		List<Product> selectAll = null;
		selectAll = productMapper.selectAll();
		if (selectAll != null) {
			// 查询所有成功
			productVo.setTList(selectAll);
			productVo.setSelect(true);
			productVo.setMessage("查询成功");
		} else {
			productVo.setMessage("查询失败");
		}
		return productVo;
	}

	@Override
	public ResultVo<Product> select(Integer productId) {
		ResultVo<Product> productVo = new ResultVo<Product>();
		Product product = null;
		product = productMapper.selectByPrimaryKey(productId);
		if (product != null) {
			productVo.setT(product);
			productVo.setSelect(true);
			productVo.setMessage("查询成功");
		} else {
			productVo.setMessage("查询失败");
		}
		return productVo;
	}

	@Override
	public ResultVo<Product> delete(Integer productId) {
		ResultVo<Product> resultVo = new ResultVo<Product>();
		int i = productMapper.delete(productId);
		if (i == 1) {
			resultVo.setDelete(true);
			resultVo.setMessage("删除成功");
		} else {
			throw new RuntimeException();
		}
		return resultVo;
	}

	@Override
	public ResultVo<Product> deleteProducts(List<Integer> productIds) {
		ResultVo<Product> resultVo = new ResultVo<Product>();
		for (Integer productId : productIds) {
			productMapper.delete(productId);
		}
		resultVo.setDelete(true);
		resultVo.setMessage("批量删除成功");
		return resultVo;
	}
}
